package com.prinxchengshan.reportbase.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @ClassName EmailUtil
 * @Description 邮件发送工具
 * @Author Sophia
 * @Date 2022/4/6 16:06
 */
@Component
public class EmailUtil {

    @Value("${spring.mail.from}") // 从application.yml配置文件中获取
    private String from; // 发送发邮箱地址

    @Autowired(required = false)
    private JavaMailSender mailSender;

    /**
     * 发送纯文本邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容（发送内容）
     */
    public void sendMessage(String to, String subject, String content) {
        // 创建一个邮件对象
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from); // 设置发送发
        msg.setTo(to); // 设置接收方
        msg.setSubject(subject); // 设置邮件主题
        msg.setText(content); // 设置邮件内容
        // 发送邮件
        mailSender.send(msg);
    }

    /**
     * 发送带附件的邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容（发送内容）
     * @param files 文件数组 // 可发送多个附件
     */
    public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            if (files != null && files.length > 0) { // 添加附件（多个）
                for (File file : files) {
                    helper.addAttachment(file.getName(), file);
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }
    /**
     * 发送带附件的邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容（发送内容）
     * @param file 单个文件
     */
    public void sendMessageCarryFile2(String to, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送方
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }


    public void sendMessageCarryFile(String to, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送方
            InternetAddress to1 = new InternetAddress("zlzhou@prinxchengshan.com");
            // InternetAddress to2 = new InternetAddress("yuwang1@prinxchengshan.com");
            // InternetAddress to3 = new InternetAddress("lichen@prinxchengshan.com");
            // InternetAddress to4 = new InternetAddress("dyqi@prinxchengshan.com");
            // InternetAddress to5 = new InternetAddress("chyxu@prinxchengshan.com");
            // InternetAddress to6 = new InternetAddress("xfsui@prinxchengshan.com");
            // InternetAddress to7 = new InternetAddress("hyta@prinxchengshan.com");

            InternetAddress[] toList = new InternetAddress[1];
            toList[0] = to1;
            // toList[1] = to2;
            // toList[2] = to3;
            // toList[3] = to4;
            // toList[4] = to5;
            // toList[5] = to6;
            // toList[6] = to7;

            helper.setTo(toList); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }

    public void sendMessageCarryFile(InternetAddress[] toList, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送方
            helper.setTo(toList); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }



    // 发送邮件
    public void sendEmail(InternetAddress[] toList , String subject , String content , File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try{
            helper  = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送方
            helper.setTo(toList); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
            mailSender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    // 设置收件人
    public InternetAddress[] setReceiver(String[] toStrings) {

        InternetAddress[]  toList = new InternetAddress[0];


        for (int i = 0; i < toStrings.length; i++) {
            toList = addToList(toList,toStrings[i]);
        }


        return toList;



    }

    public InternetAddress[] addToList(InternetAddress[] toList, String toString) {
        InternetAddress to = null;
        try{
            to = new InternetAddress(toString);
        }catch ( Exception e){
            System.out.println(e);
        }


        // 将数组转换为ArrayList
        ArrayList<InternetAddress> arrayList = new ArrayList<>(Arrays.asList(toList));

        // 向ArrayList中添加新元素
        arrayList.add(to);

        // 将ArrayList转换回数组
        InternetAddress[] newArray = arrayList.toArray(new InternetAddress[0]);

        return newArray;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }
}

